<html lang="en-US">

<head>
    
<title>使用 Prometheus 对 Go 程序进行指标采集 - 花落雨忧</title>

<meta property="og:title" content="使用 Prometheus 对 Go 程序进行指标采集 - 花落雨忧">



    



    
    <meta property="description" content="在前面的章节中，已经知道了如何对应用程序进行 Prometheus metrics 的注册和暴露，那么接下来如何让 Prometheus 对应用程序进行采集呢。
[&amp;hellip;] 首先打开先前所安装的 prometheus 软件目录：
[&amp;hellip;] $ ls LICENSE data promtool NOTICE prometheus rules console_libraries &amp;hellip;">
    <meta property="og:description" content="在前面的章节中，已经知道了如何对应用程序进行 Prometheus metrics 的注册和暴露，那么接下来如何让 Prometheus 对应用程序进行采集呢。
[&amp;hellip;] 首先打开先前所安装的 prometheus 软件目录：
[&amp;hellip;] $ ls LICENSE data promtool NOTICE prometheus rules console_libraries &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.site/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.site/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.site/js/jquery.min.js"></script>




<link href="https://www.lican.site/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>
        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/prometheus/2020-05-16-pull/">使用 Prometheus 对 Go 程序进行指标采集</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2020/05/17</a>
                                            
                                            
                                            
                                            
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <p>在前面的章节中，已经知道了如何对应用程序进行 Prometheus metrics 的注册和暴露，那么接下来如何让 Prometheus 对应用程序进行采集呢。</p>
<h2 id="设置采集配置">设置采集配置</h2>
<p>首先打开先前所安装的 prometheus 软件目录：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ ls
LICENSE                data                   promtool
NOTICE                 prometheus             rules
console_libraries      prometheus.yml         tsdb
consoles               prometheus.yml.default
</code></pre></div><p>打开并修改 prometheus.yml 文件，查看到 <a href="https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config">scrape_configs</a> 配置选项，进行如下调整：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">...
scrape_configs:
  - job_name: <span style="color:#e6db74">&#39;test01&#39;</span>
    static_configs:
    - targets: <span style="color:#f92672">[</span><span style="color:#e6db74">&#39;127.0.0.1:10001&#39;</span><span style="color:#f92672">]</span>
    scheme: http
    tls_config:
        insecure_skip_verify: false
</code></pre></div><p>先前所启动的应用程序是在本地，且端口号为 10001，协议为 http，对应的配置简述：</p>
<ul>
<li>
<p>job_name：采集的任务名。</p>
</li>
<li>
<p>static_configs.targets：设置要采集的目标对象列表。</p>
</li>
<li>
<p>scheme：采集的目标协议（例如：http、https）。</p>
</li>
<li>
<p>tls_config.insecure_skip_verify：是否跳过证书校验。</p>
</li>
</ul>
<p>常用的配置项如上几个，其默认的 metrics path 为 <code>/metrics</code>，若需要调整则增加 metrics_path 配置项进行调整就可以了，接着重新启动 prometheus 就可以了。</p>
<h2 id="其他配置项">其他配置项</h2>
<p>再返回来看看 prometheus.yaml 文件：</p>
<pre><code>global:
  scrape_interval:     15s 
  evaluation_interval: 15s


alerting:
  alertmanagers:
  - static_configs:
    - targets:
       - localhost:9093

rule_files:
   - ../rules/*.yaml

scrape_configs:
  - job_name: 'test01'
    static_configs:
    - targets: ['127.0.0.1:10001']
    scheme: http
    tls_config:
        insecure_skip_verify: false
</code></pre><ul>
<li>
<p>scrape_interval：采集间隔（频率）。</p>
</li>
<li>
<p>evaluation_interval：规则计算间隔（频率）。</p>
</li>
<li>
<p>alerting.alertmanagers：此 prometheus 所计算的 rules 所对应的 alertmanager。</p>
</li>
<li>
<p>rule_files：此 prometheus 所对应的告警规则（rules）。</p>
</li>
</ul>
<p>这里聚焦 rule_files 和 alertmanagers，其分别对应着后续章节所要学的告警规则编写和 alertmanager 的告警平台对接和使用，留个心眼即可。</p>
<h2 id="重新启动-prometheus">重新启动 Prometheus</h2>
<p>在启动 prometheus 时，可以根据自己的情况进行一些启动项的调整：</p>
<pre><code>$ ./prometheus --web.listen-address=&quot;0.0.0.0:9091&quot; --config.file=&quot;prometheus.yml&quot; 
</code></pre><p>在上述语句中，<code>--web.listen-address</code> 调整启动的监听 HOST+PORT，<code>--config.file</code> 调整 prometheus 启动时所读取的配置文件地址，更多的可以执行 <code>./prometheus --help</code> 命令进行查看。</p>
<h2 id="查看采集情况">查看采集情况</h2>
<p>在完成配置后，在经过 scrape_interval 秒后，将会自动采集到 prometheus 上，就可以在 Console 上进行查看：</p>
<p><img src="https://image.eddycjy.com/958dd2950b64c2a10a73b03864b0e153.jpg" alt="image"></p>
<p>若想要简单查看趋势，也可以通过 Graph 进行趋势分析：</p>
<p><img src="https://image.eddycjy.com/13e83b57dde665ffbbea8456efd0b146.jpg" alt="image"></p>
<p>但长久来看，还是建议使用 Grafana（后续会进行讲解）。</p>
<h2 id="总结">总结</h2>
<p>至此就完成了一个 Prometheus 对 Go 应用程序的基本采集，本章节主要是熟悉 Prometheus 拉模式的采集机制，以及对应的 Promtheus 规则编写和使用即可，虽然比较很简单，但是这块内容的知识体系的了解程度是后面所有内容的基石，建议根据<a href="https://prometheus.io/docs/introduction/overview/">官方文档</a>再进一步的学习。</p>


                                        
                                        
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.site/posts/prometheus/2020-05-16-metrics/"
                                                    rel="prev">Prometheus 四大度量指标的了解和应用</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.site/posts/where-is-proto/">Proto 代码到底放哪里？</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.site/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.vip All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>